package my.mall.front.api.controller

import jakarta.servlet.http.HttpServletRequest
import jakarta.validation.Valid
import my.mall.common.annotation.Login
import my.mall.common.pojo.CommonResp
import my.mall.common.pojo.PageParam
import my.mall.common.util.RespUtils.success
import my.mall.front.api.service.OrderService
import my.mall.front.api.pojo.order.PayOrderParam
import my.mall.front.api.pojo.order.SubmitOrderParam
import my.mall.front.api.util.OrderUtils
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/order")
class OrderController(
    private val orderService: OrderService
) {

    @PostMapping("/submit")
    fun submitOrder(@Login userId: Long, @RequestBody orderParam: @Valid SubmitOrderParam): CommonResp<*> {
        return success(orderService.submitOrder(userId, orderParam))
    }

    @PostMapping("/pay")
    fun payOrder(@Login userId: Long, @RequestBody param: @Valid PayOrderParam): CommonResp<*> {
        if (param.paymentType == null || OrderUtils.isRewardPoint(param.paymentType!!)) {
            orderService.payOrder(userId, param)
        } else {
            return success(orderService.getPaymentInfo(userId, param))
        }
        return success()
    }

    @GetMapping("/list")
    fun orderList(
        @Login userId: Long?,
        @RequestParam(value = "orderStatus", required = false) orderStatus: Int?,
        pageParam: PageParam
    ): CommonResp<*> {
        return orderService.getOrderList(userId, orderStatus, pageParam)
    }

    @GetMapping("/detail")
    fun orderDetail(@Login userId: Long, @RequestParam orderId: Long?): CommonResp<*> {
        return success(orderService.getOrderDetail(userId, orderId))
    }

    @PostMapping("/callback")
    fun callback(request: HttpServletRequest): CommonResp<*> {
        orderService.paymentCallback(request)
        return success()
    }
}
